package com.cafuc.discipline.controller;

import com.cafuc.discipline.respone.Result;
import com.cafuc.discipline.service.UserService;
import com.cafuc.discipline.anno.EncryptFilter;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


@RestController
@Api(tags = "用户管理相关接口")
public class UserController {
    @Resource
    UserService userservice;

    @PostMapping("/cafuc/user/login")
    @ApiOperation("用户登录的接口(将json格式数据进行1加密直接放入body)")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body",name="params",value ="加密后的用户信息密文串",dataType ="String")}
    )
    @EncryptFilter
    public Result login(@RequestBody HashMap<String, String> params, HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String UserAgent = request.getHeader("User-Agent");

        System.err.println(params);

        return userservice.login(params,ip,UserAgent);
    }

    @PostMapping("/cafuc/user/insert")
    @ApiOperation("用户新增的接口")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body",name="params",value ="加密后的用户信息密文串",dataType ="String")}
    )
    @EncryptFilter
    public Result insertUser(@RequestBody HashMap<String, Object> params){
        return userservice.insertNewUser(params);
    }

    @GetMapping("/cafuc/user/query")
    @ApiOperation("搜索or查询所有用户的接口(pageIndex和pageSize不能为空,其他可为空)")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query",name = "pageIndex",value ="页号",defaultValue = "1",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "pageSize",value ="页面条数",defaultValue = "10",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "role",value ="用户角色",defaultValue = "超级管理员",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "department",value ="部门",defaultValue = "后勤部",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "name",value ="用户名",defaultValue = "李华",dataType ="String")}
    )
    @EncryptFilter(decryptRequest = false)
    public Result selectUser(@ApiIgnore @RequestParam HashMap<String, Object> params){
        return userservice.selectUserList(params);
    }

    @GetMapping("/cafuc/user/getUser")
    @ApiOperation("获取当前用户信息的接口")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query",name = "userId",value ="用户id",defaultValue = "2",dataType ="String")}
    )
    @EncryptFilter(decryptRequest = false)
    public Result getUser(@RequestParam String userId){
        return userservice.selectUserNow(userId);
    }

    @GetMapping("/cafuc/user/delete")
    @ApiOperation("删除用户信息的接口")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query",name = "id",value ="用户唯一id",dataType ="String")}
    )
    @EncryptFilter(decryptRequest = false)
    public Result delete( @RequestParam String id){
        return userservice.deleteUser(id);
    }


    @GetMapping("/cafuc/user/getLog")
    @ApiOperation("查询登录日志")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query",name = "id",value ="用户唯一id",dataType ="String")}
    )
    @EncryptFilter(decryptRequest = false)
    public Result selectUserLog(@ApiIgnore @RequestParam String id){
        return userservice.selectUserLog(id);
    }
}
